<route lang="json5">
  {
    style: {
      navigationBarTitleText: '积分记录',
    },
  }
  </route>
<template>
  <view class="container">


    <!-- 分类标签 -->
    <view class="tabs">
      <view v-for="(tab, index) in tabs" :key="index" :class="['tab-item', { active: currentTab === index }]"
        @click="handleTabChange(index)">
        <text>{{ tab }}</text>
      </view>
    </view>

    <!-- 记录列表 -->
    <view class="content-card">

      <scroll-view scroll-y class="record-list" @scrolltolower="loadMore" @refresherrefresh="onRefresh"
        :refresher-enabled="true" :refresher-triggered="isRefreshing">
        <view v-if="records.length > 0">
          <view v-for="(record, index) in records" :key="index" :class="['record-card', record.type]">
            <view class="record-header">
              <text class="type-text">{{ record.type_text }}</text>
              <text :class="'amount' + record.type">{{ record.num }}</text>
            </view>
            <view class="record-info">
              <text class="id">交易ID: {{ record.sn }}</text>
              <text class="time">{{ record.create_time }}</text>
            </view>

          </view>
        </view>
        <view v-else class="empty-state">
          <uni-icons type="info" size="64" color="#999999" />
          <text class="empty-text">暂无积分记录</text>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { getPointsListAPI } from '@/service/index/index'
import { useUserStore } from '@/store/user'
const tabs = ['全部', '收入', '转换']
const currentTab = ref(0)

const isRefreshing = ref(false)

const records = ref<any[]>([])
const params = ref({
  page: 1,
  pageSize: 10,
  type: currentTab.value,
})
const getPointsList = async () => {
  const res = await getPointsListAPI(params.value)
  if (res.code === 1) {
    records.value = res.data.list
  }
}
const handleTabChange = (index: number) => {
  currentTab.value = index
  params.value.page = 1
  params.value.type = index
  getPointsList()
}

const handleBack = () => {
  uni.navigateBack()
}

const loadMore = () => {
  // 加载更多数据
  params.value.page++
  getPointsList()
}

const onRefresh = () => {
  isRefreshing.value = true
  setTimeout(() => {
    isRefreshing.value = false
  }, 1000)
}
//#ifdef H5
onMounted(() => {
  if (!useUserStore().userInfo.token) {
    uni.navigateTo({
      url: '/pages/login/login'
    })
    return
  }
  getPointsList()
})
//#endif
//#ifdef APP-PLUS ||MP-WEIXIN
onLoad(() => {
  if (!useUserStore().userInfo.token) {
    uni.navigateTo({
      url: '/pages/login/login'
    })
    return
  }
  getPointsList()
})
//#endif
</script>

<style>
page {
  height: 100%;
}

.container {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #f5f5f5;
}

.content-card {
  padding: 0 20rpx;
}

.header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 88rpx;
  background-color: #ffffff;
}

.back-icon {
  position: absolute;
  left: 30rpx;
  width: 20px;
  height: 20px;
}

.title {
  font-size: 16px;
  font-weight: 500;
  color: #333333;
}

.tabs {
  display: flex;
  height: 88rpx;
  background-color: #ffffff;
  border-bottom: 1px solid #eeeeee;
}

.tab-item {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: #666666;
}

.tab-item.active {
  font-weight: 500;
  color: #1890ff;
}

.record-list {
  flex: 1;
  padding: 20rpx 0;
  overflow: auto;
}

.record-card {
  padding: 30rpx;
  margin-bottom: 20rpx;
  background-color: #ffffff;
  border-radius: 12rpx;
}

.record-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20rpx;
}

.type-text {
  font-size: 16px;
  font-weight: 500;
  color: #333333;
}

.amount {
  font-size: 16px;
  font-weight: 500;
}

.amount0 {
  color: #07c160;
}

.amount1,
.amount3 {
  color: #ff4d4f;
}

.amount2,
.amount4 {
  color: #ff9500;
}

.record-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 16rpx;
}

.id,
.time {
  font-size: 12px;
  color: #999999;
}

.record-fee {
  font-size: 12px;
  color: #999999;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-top: 200rpx;
}

.empty-text {
  margin-top: 20rpx;
  font-size: 14px;
  color: #999999;
}
</style>
